package com.lc.builder;

import com.lc.bean.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能：
 * 作者：lc
 * 日期：2024/6/17 21:45
 */
public class BuildBase {
    private static final Logger logger = LoggerFactory.getLogger(BuildBase.class);

    public static List<String> headerInfoList = new ArrayList<>();

    public static void execute() throws IOException {
        // 生成枚举
        headerInfoList.add("package " + Constants.PACKAGE_ENUMS);
        build(headerInfoList, "DateTimePatterEnum", Constants.PATH_ENUMS);
        headerInfoList.clear();

        headerInfoList.add("package " + Constants.PACKAGE_UTILS + "\n");
        build(headerInfoList, "DateUtils", Constants.PATH_UTILS);
        headerInfoList.clear();

        // 生成baseMapper
        headerInfoList.add("package " + Constants.PACKAGE_MAPPER);
        build(headerInfoList, "BaseMapper", Constants.PATH_MAPPER);
        headerInfoList.clear();
        // 生成pageSize枚举
        headerInfoList.add("package " + Constants.PACKAGE_ENUMS);
        build(headerInfoList, "PageSize", Constants.PATH_ENUMS);
        headerInfoList.clear();
        // 生成simplePage
        headerInfoList.add("package " + Constants.PACKAGE_QUERY);
        headerInfoList.add("import " + Constants.PACKAGE_ENUMS + ".PageSize");
        build(headerInfoList, "SimplePage", Constants.PATH_QUERY);
        headerInfoList.clear();
        // 生成BaseQuery
        headerInfoList.add("package " + Constants.PACKAGE_QUERY);
        build(headerInfoList, "BaseQuery", Constants.PATH_QUERY);
        headerInfoList.clear();
        // 生成PaginationResultVO
        headerInfoList.add("package " + Constants.PACKAGE_VO);
        build(headerInfoList, "PaginationResultVO", Constants.PATH_VO);
        headerInfoList.clear();
        // 生成BusinessException
        headerInfoList.add("package " + Constants.PACKAGE_EXCEPTION);
        headerInfoList.add("import " + Constants.PACKAGE_ENUMS+".ResponseCodeEnum");
        build(headerInfoList, "BusinessException", Constants.PATH_EXCEPTION);
        headerInfoList.clear();
        // 生成ResponseCodeEnum
        headerInfoList.add("package " + Constants.PACKAGE_ENUMS);
        build(headerInfoList, "ResponseCodeEnum", Constants.PATH_ENUMS);
        headerInfoList.clear();
        // 生成ResponseCodeEnum
        headerInfoList.add("package " + Constants.PACKAGE_VO);
        build(headerInfoList, "ResponseVO", Constants.PATH_VO);
        headerInfoList.clear();
        //todo  生成ABaseController
        headerInfoList.add("package " + Constants.PACKAGE_CONTROLLER);
        headerInfoList.add("import " + Constants.PACKAGE_VO+".ResponseVO");
        headerInfoList.add("import " + Constants.PACKAGE_ENUMS+".ResponseCodeEnum");
        build(headerInfoList, "ABaseController", Constants.PATH_CONTROLLER);
        headerInfoList.clear();
        // 生成AGlobalExceptionHandlerController
        headerInfoList.add("package " + Constants.PACKAGE_CONTROLLER);
        headerInfoList.add("import " + Constants.PACKAGE_VO+".ResponseVO");
        headerInfoList.add("import " + Constants.PACKAGE_ENUMS+".ResponseCodeEnum");
        headerInfoList.add("import " + Constants.PACKAGE_EXCEPTION+".BusinessException");
        build(headerInfoList, "AGlobalExceptionHandlerController", Constants.PATH_CONTROLLER);
        headerInfoList.clear();
    }

    private static void build(List<String> headerInfoList, String fileName, String outPutPath) throws IOException {
        File folder = new File(outPutPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        File javaFile = new File(outPutPath, fileName + ".java");

        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;

        InputStream in = null;
        InputStreamReader inr = null;
        BufferedReader br = null;

        try {
            out = Files.newOutputStream(javaFile.toPath());
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);

            // todo
            String templatePath = BuildBase.class.getClassLoader().
                    getResource("template/" + fileName + ".txt").getPath();

            in = new FileInputStream(templatePath);
            inr = new InputStreamReader(in, "utf-8");
            br = new BufferedReader(inr);
            for (String headerInfo :
                    headerInfoList) {
                bw.write(headerInfo + ";\n");
                bw.newLine();
            }

            String lineInfo = null;

            while ((lineInfo = br.readLine()) != null) {
                bw.write(lineInfo);
                bw.newLine();
            }
            /**
             * 确保所有已经写入的内容都被实际写入到文件或其他输出目标，而不是仅仅保留在缓冲区中
             */
            bw.flush();

        } catch (Exception e) {
            logger.error("基础类{}创建失败！", fileName, e);
        } finally {
            if (br != null) {
                br.close();
            }
            if (inr != null) {
                inr.close();
            }
            if (in != null) {
                in.close();
            }
            if (bw != null) {
                bw.close();
            }

            if (outw != null) {
                outw.close();
            }
            if (out != null) {
                out.close();
            }
        }

    }
}
